<?php 
	//Configure::write('debug', 2);
?>
<h1>All Orders by <?=ucwords($sorting[0][0]);?></h1>
<h2>for the Drop-off on <?=date('m-d-Y', strtotime($delivery['Delivery']['delivery_open']))?></h2>
<div class="breadcrumbs">
<p><?php
if (isset($neighbors['prev'])) {
	echo $html->link('<< Go Back to ' . date('m-d-Y', strtotime($neighbors['prev']['Delivery']['delivery_open'])), '/basketItems/showAll/' . $sorting[0][0] . '/delivery:' . $neighbors['prev']['Delivery']['delivery_id']);
}
echo ' | ';
if (isset($neighbors['next'])) {
	echo $html->link('Go Forward to ' . date('m-d-Y', strtotime($neighbors['next']['Delivery']['delivery_open'])) . ' >>', '/basketItems/showAll/' . $sorting[0][0] . '/delivery:' . $neighbors['next']['Delivery']['delivery_id']);
}
?>
</p>
<p>View By: <?=$html->link(ucwords($sorting[1][0]), '/basketItems/showAll/' . $sorting[1][0]);?> | <?=$html->link(ucwords($sorting[0][0]), '/basketItems/showAll/' . $sorting[0][0]);?></p>
</div>
<table>

<?php
$now = time();
$p = '';
$m = '';
$total = 0;
$r_total = 0;
$i_total = 0;
$t_total = 0;
$subTotal = 0;
$taxTotal = 0;
$m_fee = 0;
$m_fees_total = 0;
$member_message = '';
$df = '%B %e, %Y';
$flipper = ($sorting[0][0] == 'member') ? 1 : -1;
$comp = '';
foreach ($lines as $line) {
	$comp = $line[$sorting[0][0]][$sorting[0][1]];
	if ($sorting[0][0]=='member') {
		$comp = $line['member']['first_name'] . ' ' . $comp;
	}
	if ($p != $comp) {
		if ($i_total > 0) {
			//echo '<tr><td class="subtotal" colspan="6">' . ucwords($p) . ' Total: $' . number_format($r_total,2) . '</td></tr>' . "\n";
			if ($sorting[0][0] == 'member') {
				$surcharge = round($i_total * (MEMB_SURCHARGE/100))/100;
				//$memb_fee = 
			} else {
				$surcharge = -round($i_total * (PROD_SURCHARGE/100))/100;
			}
			$inv_total = ($i_total/100) + $surcharge + ($t_total/100);
			
			echo '<tr><td class="subtotal" colspan="6">';
			echo '<p>', $member_message , '</p>', "\n";
			echo '<p>SubTotal: $' . number_format($i_total/100,2) . '</p>';
			echo '<p>Co-Op Surcharge (' . PROD_SURCHARGE . '%): $' . number_format($surcharge,2) . '</p>';
			echo '<p>Membership Fee: $' . number_format(($flipper * $m_fee),2) . '</p>';
			echo '<p>Sales Tax: $' . number_format($t_total/100,2) . '</p>';
			echo '<p>Total: $' . number_format($inv_total + ($flipper * $m_fee),2) . '</p>';
			echo '</td></tr>';
			$total += $inv_total;
			echo '</table>' . "\n";
			$r_total = 0;
			$i_total = 0;
			$t_total = 0;
		}
		$p = $comp;
		$mtime = strtotime($line[$sorting[0][0]]['paid_fee'] . ' +' . Configure::read('Membership.valid_for'));
		$ptime = strtotime(date('r', $mtime) . ' -' . Configure::read('Membership.ask_for_fee'));
		$due = strftime($df,$mtime);
		$member_message = '';
		if ($now > $mtime) {
			$member_message = 'Your membership fee of $' . $line[$sorting[0][0]]['memb_amt_owed'] . ' is due as of ' . $due . '.  Your total is adjusted below.';
		} else if ($now > $ptime) {
			$member_message = 'Your membership fee of $' . $line[$sorting[0][0]]['memb_amt_owed'] . ' will be due '. $due . '.  Your total is adjusted below, but we can waive it for this invoice if you prefer.';
		}else {
			$member_message = 'Your membership is paid through ' . $due . '.';
		}
		$m_fee = (time() > $ptime) ? $line[$sorting[0][0]]['memb_amt_owed'] : 0;
		$m_fees_total += $m_fee;
		echo '
<div class="print-only">';
		echo '
<h2>' . ucwords($sorting[0][0]) . ' Invoice</h2>';

		if ($sorting[0][0] == 'member') {
			$mInfo = $this->requestAction('/members/getMemberInfo/' . $line['member']['member_id']);
			echo $this->renderElement('memberVcard',array('m' => $mInfo['Member']));
			
			//pr($mInfo[$p]);
		}
		echo '<h3>West Michigan Coop</h3>
</div>
<table>';
	 	echo '<tr><th colspan="6">' . ucwords($p) . '</th></tr>' . "\n";
		echo '<tr><th>Product</th>
<th>Qty.</th>
<th>Price/Unit</th>
<th>Weight</th>
<th>Tax</th>
<th>Total</th>
</tr>' . "\n";
	}
	//calculate weight
	if ($line['basket_items']['weight'] == 0) {
		if ($line['product_list']['random_weight'] != 0) {
			if ($line['product_list']['minimum_weight'] == 0 && $line['product_list']['maximum_weight'] == 0) { // weights should not be averaged
				$line['basket_items']['weight'] = 1;
			} else {
				$line['basket_items']['weight'] = ($line['product_list']['minimum_weight'] + $line['product_list']['maximum_weight']) / 2;
			}
		}
	}
	//calculate total
	$line['subtotal'] = ($line['basket_items']['missing']) ? 0 : $line['orderables']['price'] * $line['basket_items']['quantity'];
	$line['subtotal'] = ($line['basket_items']['weight'] == 0) ? $line['subtotal'] : $line['subtotal'] * $line['basket_items']['weight'];
	$line['tax'] = ($line['product_list']['tax_exempt']) ? 0 : $line['subtotal'] * SALES_TAX;
	$m_comp = ($sorting[1][0] == 'member') ? $line[$sorting[1][0]][$sorting[1][1]] . ', ' . $line['member']['first_name'] : $line[$sorting[1][0]][$sorting[1][1]];
	if (($m != $m_comp) || ($r_total == 0)) {
		$m = ($sorting[1][0] == 'member') ? $line[$sorting[1][0]][$sorting[1][1]] . ', ' . $line['member']['first_name'] : $line[$sorting[1][0]][$sorting[1][1]];
		echo '<tr><th colspan="6" class="newSubGroup">' . ucwords($m) . '</th></tr>' . "\n";
	}
	//echo $sorting[1][0],$sorting[1][1];
	//echo 'BN: '. $line[$sorting[1][0]][$sorting[1][1]];
	$q = ($line['basket_items']['missing']) ? 0 : $line['basket_items']['quantity'];
	echo '<tr><td>';
	if ($line['basket_items']['missing']) {
		echo '<em>' . $line['product_list']['product_name'] . '</em>';
	} else {
		echo $line['product_list']['product_name'];
	}
	echo '</td>';
	echo '<td>' . $q . '</td>';
	echo '<td>$' . number_format($line['orderables']['price'],2) . '/' . $line['orderables']['priced_by'] . '</td>';
	$w = ($line['basket_items']['weight'] == 0) ? 'N/A' : number_format($line['basket_items']['weight'],2) . ' ' . $line['orderables']['priced_by'] . '(s)';
	echo '<td>' . $w . '</td>';
	echo '<td>$' . number_format($line['tax'],2) . '</td>';
	echo '<td>$' . number_format($line['subtotal'] + $line['tax'],2) . "</td></tr>\n";
	$tot = round($line['subtotal']*100);
	$tax = round($line['tax']*100);
	//$total += $line['subtotal'] + $line['tax'];
	$i_total += $tot;
	$t_total += $tax;
	$r_total += ($tot+$tax)/100;
	$subTotal += $tot;
	//$r_total += $line['subtotal'] + $line['tax'];
	$taxTotal += $tax;
}
//print_r($sorting);
//echo '<tr><td class="subtotal" colspan="6">' . ucwords($p) . ' Total: $' . number_format($r_total,2) . '</td></tr>' . "\n";



			//echo '<tr><td class="subtotal" colspan="6">' . ucwords($p) . ' Total: $' . number_format($r_total,2) . '</td></tr>' . "\n";
			if ($sorting[0][0] == 'member') {
				$surcharge = round($i_total * (MEMB_SURCHARGE/100))/100;
			} else {
				$surcharge = -round($i_total * (PROD_SURCHARGE/100))/100;
			}
			$inv_total = ($i_total/100) + $surcharge + ($t_total/100);
			
			echo '<tr><td class="subtotal" colspan="6">';
			echo '<p>', $member_message , '</p>', "\n";
			echo '<p>SubTotal: $' . number_format($i_total/100,2) . '</p>';
			echo '<p>Coop Surcharge (' . PROD_SURCHARGE . '%): $' . number_format($surcharge,2) . '</p>';
			echo '<p>Membership Fee: $' . number_format(($flipper * $m_fee),2) . '</p>';
			echo '<p>Sales Tax: $' . number_format($t_total/100,2) . '</p>';
			echo '<p>Total: $' . number_format($inv_total + ($flipper * $m_fee),2) . '</p>';
			echo '</td></tr>';
			$total += $inv_total;
			?>
</table>
<?='<p>SubTotal: $' . number_format($subTotal/100,2) . '</p>';?>
<?='<p>Surcharge Total: $' . number_format($total - $taxTotal/100 - $subTotal/100,2) . '</p>';?>
<?='<p>Tax Total: $' . number_format($taxTotal/100,2) . '</p>';?>
<?='<p>Total: $' . number_format($total,2) . '</p>';?>
<?='<p>Membership Fees Total: $' . number_format($m_fees_total,2) . '</p>';?>
<?='<p>Grand Total: $' . number_format($m_fees_total + $total,2) . '</p>';?>
